package com.tledu.production.controller.crafts;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.mapper.crafts.ProcessMapper;
import com.tledu.production.model.crafts.ProcessModel;
import com.tledu.production.model.crafts.TechnologyModel;
import com.tledu.production.service.crafts.ProcessService;
import lombok.AllArgsConstructor;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/process")
public class ProcessController {
    private final ProcessService processService;
    @RequestMapping("/pagePro")
    @ResponseBody
    //查询
    public AjaxResult<PageInfo<ProcessModel>>page(HttpServletRequest req, Integer pageNum, Integer pageSize, String condition, String conditionName){
        if (pageNum == null)pageNum = CommonConstant.PAGENUM;
        if (pageSize == null)pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum,pageSize);
        List<ProcessModel> list = null;
        PageInfo<ProcessModel> page = null;
        //分页
        if (conditionName==null||"".equals(conditionName)){
            list=processService.list();
            page= PageInfo.of(list);
        }else{
            //搜索
            list= processService.selectByName(condition,conditionName);
            page=PageInfo.of(list);
        }

        return new AjaxResult<>(page);
    }
    //删除
    @RequestMapping("delpro")
    public AjaxResult<String> delpro(String processId){
        processService.delPro(processId);
        return new AjaxResult<>("删除成功");
    }
    //批量删除
    @GetMapping("/delproAll")
    @ResponseBody
    public AjaxResult<String> batchDel(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            processService.delPro(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
    //添加
    @RequestMapping("/insertpro")
    @ResponseBody
    public AjaxResult<Void> insertpro(ProcessModel processModel){
        processService.insertpro(processModel);
        return new AjaxResult<>();
    }
    //编辑
    @RequestMapping("/updatepro")
    @ResponseBody
    public AjaxResult<Void>updatepro(ProcessModel processModel){
        processService.updatepro(processModel);
        return new AjaxResult<>();
    }
}
